package standart;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.SocketException;

public class Client {
	String ip;
	int port;

	public Client(String ip, int port) {
		this.ip = ip;
		this.port = port;

	}

	public void SendDataToServer(String data, String args) throws IOException {
		try (Socket clientSoc = new Socket(ip, port)) {

			InputStream sinClient = clientSoc.getInputStream();
			OutputStream soutClient = clientSoc.getOutputStream();

			DataInputStream inClient = new DataInputStream(sinClient);
			DataOutputStream outClient = new DataOutputStream(soutClient);

			outClient.writeUTF(data);
			outClient.flush();

			String s = inClient.readUTF();
			if (s.equals("200OK")) {
				System.out.println("Connection is established");
				
				outClient.writeUTF(args);
				outClient.flush();
				String decrData = inClient.readUTF();
				System.out.println("Server decrypt your message: "+ decrData);
				
			} else {
				System.out
						.println("Connection is not established, bad certificate");
			}
			
			

			/*if (s.equals("Key is added")) {
				System.out.println("Connection is established");
			} else {
				System.out.println("Something goes wrong, key is not added");
			}*/

		}
		catch(SocketException se){
			System.out.println("Server close the connection");
		}
	}
}
